package com.bop.dehong.login;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.StringUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.ToubiaoActivity;
import com.bop.dehong.http.model.ForgetGetPhoneCodeModel;
import com.bop.dehong.http.model.ForgotPasswordModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.info.ForgotPasswordInfo;
import com.bop.dehong.util.CodeUtil;
import com.bop.dehong.util.CommonDialog;
import com.bop.dehong.util.CommonDialog.OnLeftClickListener;
import com.bop.dehong.webview.WebViewActivity;
import com.bop.dehong.widget.TimeButton;


public class ForgotPasswordActivity extends BaseActivity {
	private Activity mActivity;
	private TimeButton forgot_btn_phonecode;
	private EditText mPhone, vc_code, mPhonecode;
	private TextView mNext;
	private ImageView vc_image;
	private ForgotPasswordModel mForgotPwdModel;
	private ForgetGetPhoneCodeModel mGetPhoneCodeModel;
	private String getCode;
	private final int REQUEST_RESET = 100;
	private String phone;
	private CodeUtil util;

	private void getPhonecode(){
		final String phone = getStr4TextView(mPhone);
		if (StringUtils.isEmpty(phone)) {
			ToastUtils.show(this, "手机号码不能为空");
			return;
		}
		if (!RegexChk.checkCellPhone(phone)) {
			ToastUtils.show(this, "手机号码格式不对");
			return;
		}
		showProgressDialog();
		if (mGetPhoneCodeModel == null) {
			mGetPhoneCodeModel = new ForgetGetPhoneCodeModel(this, getPhoneCodeModelCallBack);
		}
		mGetPhoneCodeModel.doNet(phone);
	}
	RequestCallBack<EmptyInfo> getPhoneCodeModelCallBack = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			// TODO Auto-generated method stub
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			// TODO Auto-generated method stub
			super.onSuccess(info);
			ToastUtils.show(ForgotPasswordActivity.this, "发送成功");
			forgot_btn_phonecode.start();
		}
		
	};


	private void getNext() {
		phone = getStr4TextView(mPhone);
		String phonecode = getStr4TextView(mPhonecode);
		if (StringUtils.isEmpty(phone)) {
			ToastUtils.show(mActivity, "手机号码不能为空!!!");
			return;
		}
		if (!RegexChk.checkCellPhone(phone)) {
			ToastUtils.show(this, "手机号码格式不对");
			return;
		}
		if (phonecode.length() != 6) {
			ToastUtils.show(this, "短信验证码位数不对");
			return;
		}
		String v_code = getStr4TextView(vc_code);
		if(v_code == null || v_code.equals("")){
			ToastUtils.show(mActivity, "没有填写验证码");
			return;
		}
		if (v_code.length() != 4) {
			ToastUtils.show(mActivity, "验证码位数不对");
			return;
		}
		if(!v_code.equalsIgnoreCase(getCode)){
			ToastUtils.show(mActivity, "验证码填写不正确");
			return;
		}
		showProgressDialog();
		if (mForgotPwdModel == null) {
			mForgotPwdModel = new ForgotPasswordModel(this, forgotCallBack);
		}
		mForgotPwdModel.doNet(phone, phonecode);
	}
	
	RequestCallBack<ForgotPasswordInfo> forgotCallBack = new RequestCallBack<ForgotPasswordInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(ForgotPasswordInfo info) {
			super.onSuccess(info);
			Intent intent = new Intent(mActivity, ResetPasswordActivity.class);
			intent.putExtra(Config.CODE, info.uid + "");
			intent.putExtra("username", phone);
			startActivityForResult(intent, REQUEST_RESET);
		}
		
	};
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		if (requestCode == REQUEST_RESET) {
			setResult(RESULT_OK, data);
			finishCurrentActivity();
		}
	};

	
	@Override
	protected void initTop() {
		View mView = getView(R.id.forgot_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("忘记密码");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		mPhone = getView(R.id.forgot_input_phone);
		mPhonecode = getView(R.id.forgot_phonecode);
		forgot_btn_phonecode = getView(R.id.forgot_btn_phonecode);
		forgot_btn_phonecode.setTextAfter("秒").setTextBefore("获取短信验证码").setLenght(60 * 1000);
		mNext = getView(R.id.forgot_next);
		vc_image = getView(R.id.vc_image);
		util = CodeUtil.getInstance();
		vc_image.setImageBitmap(util.getBitmap());
		vc_code = getView(R.id.vc_code);
		getCode = util.getCode(); //获取显示的验证码
		LogUtils.e("info", getCode+"----");
		registerOnClickListener(mNext,forgot_btn_phonecode, vc_image, getView(R.id.forgot_callphone));
	}

	@Override
	protected void initData() {
		mActivity = ForgotPasswordActivity.this;
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.activity_forgot_password;
	}
	
	@Override
	protected void onDestroy() {
		forgot_btn_phonecode.onDestroy();
		super.onDestroy();
	}


	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.forgot_btn_phonecode:
			getPhonecode();	
			break;
		case R.id.forgot_next:
			getNext();
			break;
		case R.id.vc_image:
			util = CodeUtil.getInstance();
			vc_image.setImageBitmap(util.getBitmap());
			getCode = util.getCode();
			break;
		case R.id.forgot_callphone:
			Intent intent = new Intent(Intent.ACTION_CALL);  
			intent.setData(Uri.parse("tel:"+getResources().getString(R.string.contact_phone)));  
			startActivity(intent);  
			break;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
}
